package com.unitd.comm.utils;

import com.unitd.frame.spring.SpringInstanceFactory;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Iterator;
import java.util.Set;

/**
 * @desc Cache工具类
 * @filename CacheUtils.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2017/5/13
 */
public class CacheUtils {

	private static Logger logger = LoggerFactory.getLogger(CacheUtils.class);
	private static CacheManager cacheManager = SpringInstanceFactory.getInstance(CacheManager.class);

	private static final String SYS_CACHE = "sysCache";

	/**
	 * @desc 获取SYS_CACHE缓存
	 * @param key CACHE缓存key
	 * @return CACHE缓存对象
	 */
	public static Object get(String key) {
		return get(SYS_CACHE, key);
	}

	/**
	 * @desc 获取SYS_CACHE缓存, 如果不存在给予默认值
	 * @param key          CACHE缓存key
	 * @param defaultValue 默认值
	 * @return CACHE缓存对象
	 */
	public static Object get(String key, Object defaultValue) {
		Object value = get(key);
		return value != null ? value : defaultValue;
	}

	/**
	 * @desc 写入SYS_CACHE缓存
	 * @param key CACHE缓存key
	 */
	public static void put(String key, Object value) {
		put(SYS_CACHE, key, value);
	}

	/**
	 * @desc 写入缓存
	 * @param cacheName CACHE缓存名称
	 * @param key       CACHE缓存key
	 */
	public static void put(String cacheName, String key, Object value) {
		getCache(cacheName).put(getKey(key), value);
	}

	/**
	 * @desc 从SYS_CACHE缓存中移除
	 * @param key CACHE缓存key
	 */
	public static void remove(String key) {
		remove(SYS_CACHE, key);
	}

	/**
	 * @desc 从缓存中移除
	 * @param cacheName CACHE缓存名称
	 * @param key       CACHE缓存key
	 */
	public static void remove(String cacheName, String key) {
		getCache(cacheName).remove(getKey(key));
	}

	/**
	 *  @desc 从缓存中移除所有
	 * @param cacheName CACHE缓存名称
	 */
	public static void removeAll(String cacheName) {
		Cache<String, Object> cache = getCache(cacheName);
		Set<String> keys = cache.keys();
		for (Iterator<String> it = keys.iterator(); it.hasNext(); ) {
			cache.remove(it.next());
		}
		logger.info("清理缓存： {} => {}", cacheName, keys);
	}

	/**
	 * @desc 从指定缓存中按照key获取缓存
	 * @param cacheName CACHE缓存名称
	 * @param key       CACHE缓存key
	 * @return Object
	 */
	public static Object get(String cacheName, String key) {
		return getCache(cacheName).get(getKey(key));
	}

	/**
	 * @desc 从指定缓存中按照key获取缓存,如果不存在则给予默认值
	 * @param cacheName    CACHE缓存名称
	 * @param key          CACHE缓存key
	 * @param defaultValue 默认值
	 * @return Object
	 */
	public static Object get(String cacheName, String key, Object defaultValue) {
		Object value = get(cacheName, getKey(key));
		return value != null ? value : defaultValue;
	}


	/**
	 * @desc 获取缓存键名，多数据源下增加数据源名称前缀
	 * @param key CACHE缓存key
	 * @return String
	 */
	private static String getKey(String key) {
//		String dsName = DataSourceHolder.getDataSourceName();
//		if (StringUtils.isNotBlank(dsName)){
//			return dsName + "_" + key;
//		}
		return key;
	}

	/**
	 * @desc 获得一个Cache，没有则显示日志。
	 * @param cacheName CACHE缓存名称
	 * @return Cache
	 */
	private static Cache<String, Object> getCache(String cacheName) {
		Cache<String, Object> cache = cacheManager.getCache(cacheName);
		if (cache == null) {
			throw new RuntimeException("当前系统中没有定义“" + cacheName + "”这个缓存。");
		}
		return cache;
	}
}